package main

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"github.com/Caisin/sfile/utils"
	"github.com/Caisin/sfile/utils/git"
	"github.com/Caisin/sfile/utils/timeUtil"
	"github.com/flopp/go-findfont"
	"io/ioutil"
	"os"
	"path/filepath"
	"runtime"
	"strings"
)

var button *widget.Button
var delBtn *widget.Button

func main() {
	a := app.New()
	w := a.NewWindow("GitCheck")
	accts := widget.NewEntry()
	accts.SetText("asset,score,invoice,ams,bill,pay,misc")
	proDir := widget.NewEntry()
	proDir.SetText("E:/work/Asiainfo/south-china/south-china-hnan")
	updateListDir := widget.NewEntry()
	updateListDir.SetText("E:/work/Asiainfo/south-china/code/hnan/update-manager")
	timeStr := widget.NewEntry()
	labe := widget.NewLabel("执行完会显示结果,出来结果后结果在剪切板,可以直接粘贴")
	timeStr.SetText(timeUtil.GetPreDate())
	// 初始化表单，用来显示数据
	form := widget.NewForm(
		&widget.FormItem{Text: "模块:", Widget: accts},
		&widget.FormItem{Text: "代码目录:", Widget: proDir},
		&widget.FormItem{Text: "updateList目录:", Widget: updateListDir},
		&widget.FormItem{Text: "开始时间:", Widget: timeStr},
	)
	button = widget.NewButton("执行", func() {
		getAccts := accts.Text
		getProDir := proDir.Text
		getUpdateListDir := updateListDir.Text
		getTimeStr := timeStr.Text
		button.SetText("执行中...")
		s := checkGit(getAccts, getProDir, getUpdateListDir, getTimeStr)
		labe.SetText(s)
		w.Clipboard().SetContent(labe.Text + "\n" + s)
		button.SetText("执行")
	})

	delBtn = widget.NewButton("删除本地分支", func() {
		getProDir := proDir.Text
		delBtn.SetText("删除本地分支中...")
		delLocalBranches(getProDir)
		delBtn.SetText("删除本地分支")
	})
	w.SetContent(container.NewVBox(
		form,
		button,
		delBtn,
		labe,
	))
	w.Resize(fyne.NewSize(600, 300))
	w.ShowAndRun()
	_ = os.Unsetenv("FYNE_FONT")
}
func checkGit(acts string, proDir string, updateListDir string, timeStr string) string {
	git.Accts = strings.Split(acts, ",")
	git.ProDir = proDir
	msg := git.Compare(updateListDir, "updatelist-prod.txt", timeStr)
	return msg
}

func init() {
	fontPaths := findfont.List()
	for _, path := range fontPaths {
		//楷体:simkai.ttf
		//黑体:simhei.ttf
		if strings.Contains(path, "simhei.ttf") {
			_ = os.Setenv("FYNE_FONT", path)
			break
		}
	}
}
func delLocalBranches(path string) {
	fu := func(p string, n string) bool {
		return n == "config"
	}
	files := utils.GetFiles(path, fu)
	switch runtime.GOOS {
	case `linux`:
		delBranches(files)
	case `windows`:
		delBranches(files)
	}
}

func delBranches(files []string) {
	for _, f := range files {
		if file, err := ioutil.ReadFile(f); err == nil {
			str := string(file)
			lines := strings.Split(str, "\n")
			for _, line := range lines {
				index := strings.Index(line, "[branch ")
				if index == 0 {
					//fmt.Println(line)
					dir := filepath.Dir(filepath.Dir(f))
					branch := strings.ReplaceAll(strings.ReplaceAll(line, "[branch \"", ""), "\"]", "")
					if strings.Index(branch, "/") > 0 {
						git.RunGitCommand(dir, "branch", "-D", branch)
						//fmt.Printf("git -C %s branch -D %s\n", dir, branch)
					}
				}
			}
		}
	}
}
